Sesion08/Reto-01Usaremos la base de mtcars y crearemos un scatter.smooth para ver la relacion entre peso (variable wt) y millas por galon (variable mpg). Despues checaremos la correlacion entre ambas variables y finalmente crearemos un modelo lineal, lo visualizaremos y sacaremos su resumen.
Usaremos la base de “cars” ya disponible en R. Mediante un scatterplot podremos ver la relacion entre distancia y velocidad. La funcion para crearlo es scatter.smooth(x,y) y podemos anadirle un titulo al grafico con la funcion, main=""
library(corrplot)
corrplot 0.84 loaded
Luego buscaremos la correlacion entre ambas variables:
cor(mtcars$wt, mtcars$mpg)
[1] -0.8676594
modlin <- lm(wt ~ mpg, data=mtcars)
print(modlin)
Call:
lm(formula = wt ~ mpg, data = mtcars)
Coefficients:
(Intercept) mpg
6.0473 -0.1409
summary(modlin)
Call:
lm(formula = wt ~ mpg, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-0.6516 -0.3490 -0.1381 0.3190 1.3684
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 6.04726 0.30869 19.590 < 2e-16 ***
mpg -0.14086 0.01474 -9.559 1.29e-10 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.4945 on 30 degrees of freedom
Multiple R-squared: 0.7528, Adjusted R-squared: 0.7446
F-statistic: 91.38 on 1 and 30 DF, p-value: 1.294e-10
data(mtcars)
M <- cor(mtcars)
## different color series
col1 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "white",
"cyan", "#007FFF", "blue","#00007F"))
col2 <- colorRampPalette(c("#67001F", "#B2182B", "#D6604D", "#F4A582",
"#FDDBC7", "#FFFFFF", "#D1E5F0", "#92C5DE",
"#4393C3", "#2166AC", "#053061"))
col3 <- colorRampPalette(c("red", "white", "blue"))
col4 <- colorRampPalette(c("#7F0000", "red", "#FF7F00", "yellow", "#7FFF7F",
"cyan", "#007FFF", "blue", "#00007F"))
wb <- c("white", "black")
par(ask = TRUE)
corrplot(M, method = "number", col = "black", cl.pos = "n")
corrplot(M, method = "number")
corrplot(M)
corrplot(M, order = "AOE")
corrplot(M, order = "AOE", addCoef.col = "grey")
corrplot(M, order = "AOE", col = col1(20), cl.length = 21, addCoef.col = "grey")
corrplot(M, order = "AOE", col = col1(10), addCoef.col = "grey")
corrplot(M, order = "AOE", col = col2(200))
corrplot(M, order = "AOE", col = col2(200), addCoef.col = "grey")
corrplot(M, order = "AOE", col = col2(20), cl.length = 21, addCoef.col = "grey")
corrplot(M, order = "AOE", col = col2(10), addCoef.col = "grey")
corrplot(M, order = "AOE", col = col3(100))
corrplot(M, order = "AOE", col = col3(10))
corrplot(M, method = "color", col = col1(20), cl.length = 21, order = "AOE",
addCoef.col = "grey")
corrplot(M, method = "square", col = col2(200), order = "AOE")
corrplot(M, method = "ellipse", col = col1(200), order = "AOE")
corrplot(M, method = "shade", col = col3(20), order = "AOE")
corrplot(M, method = "pie", order = "AOE")
## col = wb
corrplot(M, col = wb, order = "AOE", outline = TRUE, cl.pos = "n")
## like Chinese wiqi, suit for either on screen or white-black print.
corrplot(M, col = wb, bg = "gold2", order = "AOE", cl.pos = "n")
## mixed methods: It's more efficient if using function "corrplot.mixed"
## circle + ellipse
corrplot(M, order = "AOE", type = "upper", tl.pos = "d")
corrplot(M, add = TRUE, type = "lower", method = "ellipse", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
## circle + square
corrplot(M, order = "AOE",type = "upper", tl.pos = "d")
corrplot(M, add = TRUE, type = "lower", method = "square", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
## circle + colorful number
corrplot(M, order = "AOE", type = "upper", tl.pos = "d")
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
diag = FALSE, tl.pos = "n", cl.pos = "n")
## circle + black number
corrplot(M, order = "AOE", type = "upper", tl.pos = "tp")
corrplot(M, add = TRUE, type = "lower", method = "number", order = "AOE",
col = "black", diag = FALSE, tl.pos = "n", cl.pos = "n")
## order is hclust and draw rectangles
corrplot(M, order = "hclust")
corrplot(M, order = "hclust", addrect = 2)
corrplot(M, order = "hclust", addrect = 3, rect.col = "red")
corrplot(M, order = "hclust", addrect = 4, rect.col = "blue")
corrplot(M, order = "hclust", hclust.method = "ward.D2", addrect = 4)
## visualize a matrix in [0, 1]
corrplot(abs(M), order = "AOE", cl.lim = c(0,1))
corrplot(abs(M), order = "AOE", col = col1(20), cl.lim = c(0,1))
corrplot(abs(M), order = "AOE", col = col3(200), cl.lim = c(0,1))
## visualize a matrix in [-100, 100]
ran <- round(matrix(runif(225, -100,100), 15))
corrplot(ran, is.corr = FALSE)
corrplot(ran, is.corr = FALSE, cl.lim = c(-100, 100))
## text-labels and plot type
corrplot(M, order = "AOE", tl.srt = 45)
corrplot(M, order = "AOE", tl.srt = 60)
corrplot(M, order = "AOE", tl.pos = "d", cl.pos = "n")
corrplot(M, order = "AOE", diag = FALSE, tl.pos = "d")
corrplot(M, order = "AOE", type = "upper")
corrplot(M, order = "AOE", type = "upper", diag = FALSE)
corrplot(M, order = "AOE", type = "lower", cl.pos = "b")
corrplot(M, order = "AOE", type = "lower", cl.pos = "b", diag = FALSE)
## an animation of changing confidence interval in different significance level
## begin.animaton
par(ask = FALSE)
for (i in seq(0.1, 0, -0.005)) {
tmp <- cor.mtest(mtcars, conf.level = 1 - i)
corrplot(M, p.mat = tmp$p, low = tmp$lowCI, upp = tmp$uppCI, order = "hclust",
pch.col = "red", sig.level = i, plotCI = "rect", cl.pos = "n",
mar = c(0, 0, 1, 0),
title = substitute(alpha == x,
list(x = format(i, digits = 3, nsmall = 3))))
Sys.sleep(0.15)
}
## end.animaton